package com.spring.wx.oauth.conntroller;

import com.spring.wx.oauth.entity.UserCompanyEntity;
import com.spring.wx.oauth.entity.UserEntity;
import com.spring.wx.oauth.entity.VerifyCodeEntity;
import com.spring.wx.oauth.service.CompanyService;
import com.spring.wx.oauth.service.UserService;
import com.spring.wx.oauth.service.VerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * Copyright: yueyong (c) 2022
 *
 * @author he.shaorong
 * @version 1.0
 * @date 2022/4/29
 */
@Controller
public class TestController {


    @Autowired
    private UserService userService;
    @Autowired
    private VerifyCodeService verifyCodeService;
    @Autowired
    private CompanyService companyService;


    @GetMapping("/test")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String index() {
        return "test";
    }

    @GetMapping("/showErrorMessage")
    public String showErrorMessage(String errorMessage) {
        return "/error";
    }

    @GetMapping("/getVerifyCodeTest")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String getVerifyCodeEntityFromPhoneNumber(String mobilePhone) {

        List<VerifyCodeEntity> entityList = verifyCodeService.getVerifyCodeEntityFromPhoneNumber(mobilePhone);
        if (entityList != null && entityList.size() > 0) {
            return entityList.get(0).getVerify_code().toString();
        }
        return "failed";
    }


    @GetMapping("/MP_verify_IcSfyhqG9zdxYx2A.txt")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String verify() {
        return "IcSfyhqG9zdxYx2A";
    }

    @GetMapping("/testWx")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String testWx() {
        boolean sendWxMessage = sendWxMail("18721857670");
        return String.valueOf(sendWxMessage);
    }

    @GetMapping("/testCompany")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String testCompany(String uid) {

        UserCompanyEntity companyEntity = companyService.getCompany(Long.valueOf(uid));
        if(Objects.isNull(companyEntity)) {
            return "无记录";
        }
        return companyEntity.getCompany_name();
    }


    public boolean sendWxMail(String phoneNumber) {
        String openId = getOpenIdFromUserTable(phoneNumber);
        if(Objects.nonNull(openId)) {

            String companyName = "abc";
            LocalDate date = LocalDate.now();
            String job = "hr";
            return SendWxMessageUtil.sendMessage(companyName,  date, job,  openId);
        }

        return false;
    }


    private String getOpenIdFromUserTable(String phone) {
        List<UserEntity> results = userService.getUserEntityFromPhoneNumber(phone);
        if(results != null && results.size() >0) {
            return results.get(0).getOpenid();
        }
        return null;
    }

}
